package com.yds.longlinks.client;

import org.apache.mina.core.session.IoSession;

/**
 * 作者:     YDS
 * 创建时间: 2022/11/29 21:43
 * 页面描述:
 */
public class MinaClientSessionManager {

    private IoSession mIoSession;

    private MinaClientSessionManager() {
    }

    public static MinaClientSessionManager getInstance() {
        return SafeMode.sessionManager;
    }

    private static class SafeMode {
        private static final MinaClientSessionManager sessionManager = new MinaClientSessionManager();
    }

    public void setSession(IoSession session) {
        this.mIoSession = session;
    }

    /**
     * 将对象写到服务器
     *
     * @param obj
     */
    public void writeToServer(Object obj) {
        if (null != mIoSession) {
            mIoSession.write(obj);
        }
    }

    public void closeSession() {
        if (null != mIoSession) {
            mIoSession.closeOnFlush();
        }
    }

    public void removeSession(){
        this.mIoSession=null;
    }
}
